//
//  GKZYMajorQueryViewController.m
//  GKZYAid
//
//  Created by lanou3g on 16/2/27.
//  Copyright © 2016年 GKZYAid. All rights reserved.
//


//  Tag 值 101开始

#import "GKZYMajorQueryViewController.h"
#import "GKZYSortLeftBarButtonItemView.h"
#import "MyDefine.h"
#import "MajorButtonView.h"
#import <AFNetworking.h>
#import "Urls.h"
#import <RATreeView.h>
#import "RADataObject.h"
#import "GKZYMajorQueryModel.h"
#import "GKZYSortMajorDetailViewController.h"
#import "RADataObjectManager.h"
#import "GKZYSortViewController.h"
#import "GKZYUserSortViewController.h"
#import "MBProgressHUD.h"

@interface GKZYMajorQueryViewController ()<UIScrollViewDelegate,RATreeViewDataSource,RATreeViewDelegate>{
    
    MBProgressHUD *HUD;
}

@property(nonatomic,strong)UIScrollView *scrollView;
@property(nonatomic,strong)UISegmentedControl *segmenter;
//本科三级树状菜单
@property(nonatomic,strong)RATreeView *bachelorTableTree;
//专科三级树状菜单
@property(nonatomic,strong)RATreeView *juniorTableTree;
//本科按钮
@property(nonatomic,strong)MajorButtonView *bachelorBut;
//专科按钮
@property(nonatomic,strong)MajorButtonView *juniorBut;

@property (strong, nonatomic) NSMutableArray *data;
@property(nonatomic,strong)NSMutableArray *data0;
@property(nonatomic,strong)NSMutableArray *majorArray;
@property(nonatomic,strong)NSMutableArray *majorListArray;

//修改按键
@property(nonatomic,strong)UILabel *modifyLab;


@end

@implementation GKZYMajorQueryViewController

-(void)loadView{

    [super loadView];
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"专业查询";
    GKZYSortLeftBarButtonItemView *leftBar =[[GKZYSortLeftBarButtonItemView alloc]initWithFrame:CGRectMake(0, 0, 60, 30)];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(backToVC)];
    [leftBar addGestureRecognizer:tap];
    UIBarButtonItem *leftBBI =[[UIBarButtonItem alloc]initWithCustomView:leftBar];
    self.navigationItem.leftBarButtonItem = leftBBI;
    
    self.bachelorBut = [[MajorButtonView alloc]initWithFrame:CGRectMake(0, 64, VCWIDTH/2, 35)];
    UITapGestureRecognizer *tap1 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(bacheor)];
    [self.bachelorBut addGestureRecognizer:tap1];
    [self.bachelorBut setTheTitle:@"本科专业"];
    [self.bachelorBut setTheTitleColor:[UIColor orangeColor]andMarkColor:[UIColor orangeColor]];
    self.bachelorBut.backgroundColor = [UIColor colorWithWhite:0.1 alpha:0.1];
    [self.view addSubview:self.bachelorBut];
    
    self.juniorBut = [[MajorButtonView alloc]initWithFrame:CGRectMake(VCWIDTH/2, 64, VCWIDTH/2, 35)];
    UITapGestureRecognizer *tap2 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(junior)];
    [self.juniorBut addGestureRecognizer:tap2];
    [self.juniorBut setTheTitle:@"专科专业"];
    [self.juniorBut setTheTitleColor:[UIColor colorWithWhite:0 alpha:0.5]andMarkColor:[UIColor clearColor]];
    self.juniorBut.backgroundColor = [UIColor colorWithWhite:0.1 alpha:0.1];
    [self.view addSubview:self.juniorBut];
    
    self.scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0,99 , VCWIDTH, VCHEIGHT-99)];
    self.scrollView.contentSize = CGSizeMake(VCWIDTH*2, VCHEIGHT-99);
    self.scrollView.pagingEnabled = YES;
    self.scrollView.delegate = self;
    [self.view addSubview:self.scrollView];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:YES];
    
    if ([RADataObjectManager sharedRADataObjectManager].raDataObject != nil) {
        [self.bachelorTableTree expandRowForItem:[RADataObjectManager sharedRADataObjectManager].raDataObject];
        [RADataObjectManager sharedRADataObjectManager].raDataObject = nil;
        [RADataObjectManager sharedRADataObjectManager].ZKRadataObject = nil;
    }
    
    if ([RADataObjectManager sharedRADataObjectManager].ZKRadataObject != nil) {
        [self.juniorTableTree expandRowForItem:[RADataObjectManager sharedRADataObjectManager].ZKRadataObject];
        [RADataObjectManager sharedRADataObjectManager].ZKRadataObject = nil;
        [RADataObjectManager sharedRADataObjectManager].raDataObject = nil;
    }
    
}

- (void)viewDidLoad {
    
    [super viewDidLoad];
    self.bachelorTableTree = [[RATreeView alloc]initWithFrame:CGRectMake(0, 0, VCWIDTH, VCHEIGHT-143)];
    self.bachelorTableTree.backgroundColor = [UIColor whiteColor];
    self.bachelorTableTree.delegate = self;
    self.bachelorTableTree.dataSource = self;
    self.bachelorTableTree.tag = 101;
    self.bachelorTableTree.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.bachelorTableTree.scrollView.showsVerticalScrollIndicator = NO;
    [self.scrollView addSubview:self.bachelorTableTree];
    
    self.juniorTableTree = [[RATreeView alloc]initWithFrame:CGRectMake(VCWIDTH, 0, VCWIDTH, VCHEIGHT-143)];
    self.juniorTableTree.backgroundColor = [UIColor clearColor];
    self.juniorTableTree.delegate = self;
    self.juniorTableTree.dataSource = self;
    self.juniorTableTree.tag = 102;
    self.juniorTableTree.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.juniorTableTree.scrollView.showsVerticalScrollIndicator = NO;
    [self.scrollView addSubview:self.juniorTableTree];

    self.modifyLab = [[UILabel alloc]initWithFrame:CGRectMake(VCWIDTH -85, 33, 70, 20)];
    self.modifyLab.userInteractionEnabled = YES;
    self.modifyLab.textColor = [UIColor orangeColor];
    self.modifyLab.text = @"修改分数";
    UITapGestureRecognizer *tap5 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(collectionAction)];
    [self.modifyLab addGestureRecognizer:tap5];
    UIBarButtonItem *barBI = [[UIBarButtonItem alloc]initWithCustomView:self.modifyLab];
    self.navigationItem.rightBarButtonItem = barBI;
    [self makeData0];
    
    [self makeData];
}

//懒加载

-(NSMutableArray *)data{

    if (_data == nil) {
        _data = [[NSMutableArray alloc]init];
    }
    return _data;
}

-(NSMutableArray *)data0{

    if (_data0 == nil) {
        _data0 = [[NSMutableArray alloc]init];
    }
    return _data0;
}

//制作数据
-(void)makeData{

    HUD = [[MBProgressHUD alloc] initWithView:self.view];
    [self.view addSubview:HUD];
    HUD.dimBackground = YES;
    HUD.labelText = @"请稍等";
    [HUD showAnimated:YES whileExecutingBlock:^{
        [self dataRequest0];
    } completionBlock:^{
        
        [HUD removeFromSuperview];
        HUD = nil;
    }];
}

-(void)makeData0{
    
    HUD = [[MBProgressHUD alloc] initWithView:self.view];
    [self.view addSubview:HUD];
    HUD.dimBackground = YES;
    HUD.labelText = @"请稍等";
    [HUD showAnimated:YES whileExecutingBlock:^{
        [self dataRequest1];
    } completionBlock:^{
        
        [HUD removeFromSuperview];
        HUD = nil;
    }];
}


-(void)dataRequest0{

    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html", @"text/plain", nil];
    
    [manager GET:KbachelorURL parameters:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
        
        NSDictionary *outDictionary = responseObject[@"data"];
        
        NSArray *major_cate_listArr = outDictionary[@"major_cate_list"];//最外层大类array
        for (NSDictionary *cateDictionary in major_cate_listArr) {
            
            NSArray *sceondListArr = cateDictionary[@"major_second_cate_list"];
            self.majorListArray = [[NSMutableArray alloc]init];
            for (NSDictionary *cateDict in sceondListArr) {
                
                NSArray *majorArr = cateDict[@"major_list"];
                self.majorArray = [[NSMutableArray alloc]init];
                for (NSDictionary *majorDict in majorArr) {
                    GKZYMajorQueryModel *model = [[GKZYMajorQueryModel alloc]init];
                    [model setValuesForKeysWithDictionary:majorDict];
                    RADataObject *object2 = [RADataObject dataObjectWithName:model.major_name children:nil Model:model];
                    [self.majorArray addObject:object2];
                }
                
                RADataObject *object1 = [RADataObject dataObjectWithName:cateDict[@"major_second_cate"] children:self.majorArray Model:nil];
                [self.majorListArray addObject:object1];
            }
            RADataObject *object0 = [RADataObject dataObjectWithName:cateDictionary[@"major_cate"] children:self.majorListArray Model:nil];
            [self.data addObject:object0];
            
        }
        
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.bachelorTableTree reloadData];
            
        });
        
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"网络请求失败,错误代码为%@",error);
        
    }];
}

-(void)dataRequest1{
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html", @"text/plain", nil];
    
    [manager GET:KjuniorURL parameters:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
        
        NSDictionary *outDictionary = responseObject[@"data"];
        
        NSArray *major_cate_listArr = outDictionary[@"major_cate_list"];//最外层大类array
        for (NSDictionary *cateDictionary in major_cate_listArr) {
            
            NSArray *sceondListArr = cateDictionary[@"major_second_cate_list"];
            self.majorListArray = [[NSMutableArray alloc]init];
            for (NSDictionary *cateDict in sceondListArr) {
                
                NSArray *majorArr = cateDict[@"major_list"];
                self.majorArray = [[NSMutableArray alloc]init];
                for (NSDictionary *majorDict in majorArr) {
                    GKZYMajorQueryModel *model = [[GKZYMajorQueryModel alloc]init];
                    [model setValuesForKeysWithDictionary:majorDict];
                    RADataObject *object2 = [RADataObject dataObjectWithName:model.major_name children:nil Model:model];
                    [self.majorArray addObject:object2];
                }
                
                RADataObject *object1 = [RADataObject dataObjectWithName:cateDict[@"major_second_cate"] children:self.majorArray Model:nil];
                [self.majorListArray addObject:object1];
            }
            RADataObject *object0 = [RADataObject dataObjectWithName:cateDictionary[@"major_cate"] children:self.majorListArray Model:nil];
            [self.data0 addObject:object0];
            
        }
        
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.juniorTableTree reloadData];
        });
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"网络请求失败,错误代码为%@",error);
    }];
}


-(void)backToVC{

    [self.navigationController popViewControllerAnimated:YES];

}

-(void)junior{

    self.scrollView.contentOffset = CGPointMake(VCWIDTH, 0);
    [self.juniorBut setTheTitleColor:[UIColor orangeColor] andMarkColor:[UIColor orangeColor]];
    [self.bachelorBut setTheTitleColor:[UIColor colorWithWhite:0 alpha:0.5] andMarkColor:[UIColor clearColor]];
}

-(void)bacheor{

    self.scrollView.contentOffset = CGPointMake(0, 0);
    [self.bachelorBut setTheTitleColor:[UIColor orangeColor] andMarkColor:[UIColor orangeColor]];
    [self.juniorBut setTheTitleColor:[UIColor colorWithWhite:0 alpha:0.5] andMarkColor:[UIColor clearColor]];
}

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{

    if (scrollView.contentOffset.x == VCWIDTH) {
        
        [self.juniorBut setTheTitleColor:[UIColor orangeColor] andMarkColor:[UIColor orangeColor]];
        [self.bachelorBut setTheTitleColor:[UIColor colorWithWhite:0 alpha:0.5] andMarkColor:[UIColor clearColor]];
        
    }else{
        
        [self.bachelorBut setTheTitleColor:[UIColor orangeColor] andMarkColor:[UIColor orangeColor]];
        [self.juniorBut setTheTitleColor:[UIColor colorWithWhite:0 alpha:0.5] andMarkColor:[UIColor clearColor]];
    }
}

-(void)collectionAction{
    
    GKZYUserSortViewController *userVC = [[GKZYUserSortViewController alloc]init];
    self.navigationController.navigationBar.alpha = 1;
    [self.navigationController pushViewController:userVC animated:YES];
    
}



#pragma mark TreeView Delegate methods

-(CGFloat)treeView:(RATreeView *)treeView heightForRowForItem:(id)item{

    return 40;
}



-(NSInteger)treeView:(RATreeView *)treeView indentationLevelForRowForItem:(id)item{

    return 3 * [treeView levelForCellForItem:item];
}

#pragma mark TreeView Data Source

-(UITableViewCell*)treeView:(RATreeView *)treeView cellForItem:(id)item{

    UITableViewCell *cell = [treeView dequeueReusableCellWithIdentifier:@"cell"];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:@"cell"];
    }
    cell.textLabel.highlightedTextColor = [UIColor orangeColor];
    cell.textLabel.text = ((RADataObject*)item).name;
    cell.selected = NO;
    UIView *view = [[UIView alloc]init];
    view.backgroundColor =[UIColor whiteColor];
    cell.selectedBackgroundView = view;
    cell.layer.borderColor = [[UIColor colorWithWhite:0.1 alpha:0.1]CGColor];
    cell.layer.borderWidth = 0.5f;
    return cell;
}



- (NSInteger)treeView:(RATreeView *)treeView numberOfChildrenOfItem:(id)item{
    
    if (treeView.tag == 101) {
        
        if (item == nil) {
            return [self.data count];
        }
        RADataObject *data = item;
        return [data.children count];
        
    }else{
    
    if (item == nil) {
        return [self.data0 count];
    }
    RADataObject *data = item;
    return [data.children count];
    }
}

- (id)treeView:(RATreeView *)treeView child:(NSInteger)index ofItem:(id)item
{
    
    if (treeView.tag == 101) {
       
        RADataObject *data = item;
        if (item == nil) {
            return [self.data objectAtIndex:index];
        }
        return [data.children objectAtIndex:index];
    }else{
    
    RADataObject *data = item;
    if (item == nil) {
        return [self.data0 objectAtIndex:index];
    }
    return [data.children objectAtIndex:index];
   
    }
}

-(void)treeView:(RATreeView *)treeView didExpandRowForItem:(id)item{
    if (treeView.tag == 101){
        if ([treeView levelForCellForItem:item] == 0) {
            [RADataObjectManager sharedRADataObjectManager].raDataObject = item;
        }
   
    }else{
        if ([treeView levelForCellForItem:item] == 0) {
            [RADataObjectManager sharedRADataObjectManager].ZKRadataObject = item;
        }
    
    }
}

-(void)treeView:(RATreeView *)treeView didSelectRowForItem:(id)item{

        if ([treeView levelForCellForItem:item] == 2) {
            GKZYSortMajorDetailViewController *detail = [[GKZYSortMajorDetailViewController alloc]init];
            RADataObject *object = (RADataObject *)item;
            detail.model = object.model;
            [self.navigationController hidesBarsWhenKeyboardAppears];
            [self.navigationController pushViewController:detail animated:YES];
        }
}


-(BOOL)treeView:(RATreeView *)treeView canEditRowForItem:(id)item{

    return NO;
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
